home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 426-450 / disk_441 / dme / src / cmd3.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  458 lines

  1.  
  2. /*
  3.  * CMD3.C
  4.  *
  5.  *    (C)Copyright 1988 by Matthew Dillon, All Rights Reserved
  6.  *
  7.  *  SETFONT
  8.  *  IGNORECASE
  9.  *  SET
  10.  *  SETENV
  11.  *  UNSET
  12.  *  UNSETENV
  13.  *  CD
  14.  *  SAVECONFIG
  15.  *  FGPEN
  16.  *  TPEN
  17.  *  BGPEN
  18.  *  TITLE
  19.  *  JUSTIFY
  20.  *  UNJUSTIFY
  21.  *  MODIFIED
  22.  *  UNDELINE
  23.  */
  24.  
  25. #include "defs.h"
  26.  
  27. Prototype void do_setfont (void);
  28. Prototype void do_ignorecase (void);
  29. Prototype void do_cd (void);
  30. Prototype void do_set (void);
  31. Prototype void do_setenv (void);
  32. Prototype void do_unset (void);
  33. Prototype void do_unsetenv (void);
  34. Prototype char *getvar (char *);
  35. Prototype void loadconfig(ED *);
  36.  
  37. Prototype void do_saveconfig(void);
  38. Prototype void do_fgpen(void);
  39. Prototype void do_bgpen(void);
  40. Prototype void do_hgpen(void);
  41. Prototype void do_tpen(void);
  42. Prototype void do_justify(void);
  43. Prototype void do_title(void);
  44. Prototype void do_undeline(void);
  45. Prototype void do_unjustify(void);
  46. Prototype void do_modified(void);
  47.  
  48.  
  49. #define nomemory()  { memoryfail = 1; }
  50.  
  51. /*
  52.  *  SETFONT font size
  53.  */
  54.  
  55. void
  56. do_setfont()
  57. {
  58.     FONT *font = (FONT *)GetFont(av[1], (short)atoi(av[2]));
  59.     ED *ep = Ep;
  60.     if (font) {
  61.     text_sync();
  62.     if (ep->Font)
  63.         CloseFont(ep->Font);
  64.     ep->Font = font;
  65.     SetFont(ep->Win->RPort, font);
  66.     SetRast(ep->Win->RPort, 0);
  67.     RefreshWindowFrame(ep->Win);
  68.     set_window_params();
  69.     text_redisplay();
  70.     } else {
  71.     title("Unable to find font");
  72.     }
  73. }
  74.  
  75. void
  76. do_ignorecase()
  77. {
  78.     ED *ep = Ep;
  79.  
  80.     if (av[1][0]) {
  81.     switch(av[1][1] & 0x1F) {
  82.     case 'n'&0x1F:
  83.         ep->IgnoreCase = 1;
  84.         break;
  85.     case 'f'&0x1F:
  86.         ep->IgnoreCase = 0;
  87.         break;
  88.     case 'o'&0x1F:
  89.         ep->IgnoreCase = 1 - ep->IgnoreCase;
  90.         break;
  91.     }
  92.     if (ep->IgnoreCase)
  93.         title("Case InSensitive");
  94.     else
  95.         title("Case Sensitive");
  96.     }
  97. }
  98.  
  99. /*
  100.  *  av[1]
  101.  */
  102.  
  103. void
  104. do_cd()
  105. {
  106.     BPTR oldlock;
  107.     BPTR lock;
  108.  
  109.     oldlock = CurrentDir((BPTR)Ep->dirlock);
  110.     if (lock = Lock(av[1], SHARED_LOCK)) {
  111.     UnLock(CurrentDir(oldlock));
  112.     Ep->dirlock = (long)lock;
  113.     } else {
  114.     CurrentDir(oldlock);
  115.     Abortcommand = 1;
  116.     title("Unable to CD");
  117.     }
  118. }
  119.  
  120. /*
  121.  *  VARIABLE SUPPORT!
  122.  */
  123.  
  124. #define VARS    struct _VARS
  125. VARS {
  126.     MNODE   Node;
  127.     char    *Name;
  128.     char    *Str;
  129. };
  130.  
  131. static MLIST SList = { (MNODE *)&SList.mlh_Tail, NULL, (MNODE *)&SList.mlh_Head };
  132.  
  133. void
  134. do_set()
  135. {
  136.     VARS *v;
  137.     void do_unset();
  138.  
  139.     do_unset();
  140.     if (v = malloc(sizeof(VARS))) {
  141.     if (v->Name = malloc(strlen(av[1])+1)) {
  142.         if (v->Str = malloc(strlen(av[2])+1)) {
  143.         AddHead((LIST *)&SList, (NODE *)v);
  144.         strcpy(v->Name, av[1]);
  145.         strcpy(v->Str , av[2]);
  146.         return;
  147.         }
  148.         free(v->Name);
  149.     }
  150.     free(v);
  151.     }
  152.     nomemory();
  153. }
  154.  
  155. void
  156. do_setenv()
  157. {
  158.     SetDEnv(av[1], av[2]);
  159. }
  160.  
  161. void
  162. do_unset()
  163. {
  164.     VARS *v;
  165.  
  166.     for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
  167.     if (strcmp(v->Name, av[1]) == 0) {
  168.         Remove((NODE *)v);
  169.         free(v);
  170.         free(v->Name);
  171.         free(v->Str);
  172.         break;
  173.     }
  174.     }
  175. }
  176.  
  177. void
  178. do_unsetenv()
  179. {
  180.     char *ptr = (char *)av[1];
  181.     char *tmp = malloc(4+strlen(ptr)+1);
  182.  
  183.     if (tmp) {
  184.     strcpy(tmp, "ENV:");
  185.     strcat(tmp, ptr);
  186.     mountrequest(0);
  187.     DeleteFile(tmp);
  188.     mountrequest(1);
  189.     free(tmp);
  190.     }
  191. }
  192.  
  193. /*
  194.  *  Search (1) internal list, (2) enviroment, (3) macros.  The variable
  195.  *  is allocated with malloc().  NULL if not found.  ENV: need not exist.
  196.  */
  197.  
  198. char *
  199. getvar(find)
  200. char *find;
  201. {
  202.     char *str = NULL;
  203.     {
  204.     VARS *v;
  205.  
  206.     for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
  207.         if (strcmp(v->Name, find) == 0) {
  208.         if (str = malloc(strlen(v->Str)+1)) {
  209.             strcpy(str, v->Str);
  210.             return(str);
  211.         }
  212.         }
  213.     }
  214.     }
  215.  
  216.     mountrequest(0);
  217.     str = (char *)GetDEnv(find);
  218.     mountrequest(1);
  219.     if (str)
  220.     return(str);
  221.  
  222.     if ((str = keyspectomacro(find)) || (str = menutomacro(find))) {
  223.     char *ptr = malloc(strlen(str)+1);
  224.     if (ptr) {
  225.         strcpy(ptr, str);
  226.         return(ptr);
  227.     }
  228.     }
  229.     return(NULL);
  230. }
  231.  
  232. void
  233. do_col()
  234. {
  235.     int col;
  236.  
  237.     {
  238.     char *ptr = av[1];
  239.  
  240.     switch(*ptr) {
  241.     case '+':
  242.         col = text_colno() + atoi(ptr + 1);
  243.         if (col > 254)
  244.         col = 254;
  245.         break;
  246.     case '-':
  247.         col = text_colno() + atoi(ptr);
  248.         if (col < 0)
  249.         col = 0;
  250.         break;
  251.     default:
  252.         col = atoi(ptr) - 1;
  253.         break;
  254.     }
  255.     }
  256.     if (col > 254 || col < 0) {
  257.     Abortcommand = 1;
  258.     return;
  259.     }
  260.     while (Clen < col)
  261.     Current[Clen++] = ' ';
  262.     Current[Clen] = 0;
  263.     Ep->Column = col;
  264.     if (Ep->Column - Ep->Topcolumn >= Columns || Ep->Column < Ep->Topcolumn)
  265.     text_sync();
  266. }
  267.  
  268. void
  269. do_saveconfig()
  270. {
  271.     ED *ep = Ep;
  272.     FILE *fi;
  273.  
  274.     if (ep->iconmode == 0) {
  275.     WIN *win = ep->Win;
  276.     ep->Winx      = win->LeftEdge;
  277.     ep->Winy      = win->TopEdge;
  278.     ep->Winwidth  = win->Width;
  279.     ep->Winheight = win->Height;
  280.     }
  281.  
  282.     if (fi = fopen("s:dme.config", "w")) {
  283.     fwrite(&ep->BeginConfig, (char *)&ep->EndConfig - (char *)&ep->BeginConfig, 1, fi);
  284.     fclose(fi);
  285.     }
  286. }
  287.  
  288. void
  289. loadconfig(ep)
  290. ED *ep;
  291. {
  292.     FILE *fi;
  293.  
  294.     if (fi = fopen("s:dme.config", "r")) {
  295.     fread(&ep->BeginConfig, (char *)&ep->EndConfig - (char *)&ep->BeginConfig, 1, fi);
  296.     fclose(fi);
  297.     }
  298. }
  299.  
  300. void
  301. do_fgpen()
  302. {
  303.     ED *ep = Ep;
  304.  
  305.     ep->FGPen = atoi(av[1]);
  306. }
  307.  
  308. void
  309. do_tpen()
  310. {
  311.     ED *ep = Ep;
  312.  
  313.     ep->TPen = atoi(av[1]);
  314. }
  315.  
  316. void
  317. do_bgpen()
  318. {
  319.     ED *ep = Ep;
  320.  
  321.     ep->BGPen = atoi(av[1]);
  322. }
  323.  
  324. void
  325. do_hgpen()
  326. {
  327.     ED *ep = Ep;
  328.  
  329.     ep->HGPen = atoi(av[1]);
  330. }
  331.  
  332. /*
  333.  *  Commands submitted by Markus Wenzel
  334.  */
  335.  
  336. void
  337. do_undeline()
  338. {
  339.    do_insline();
  340.    text_load();
  341.    strcpy(Current, Deline);
  342.    text_sync();
  343.    text_displayseg(Ep->Line - Ep->Topline, 1);
  344. }
  345.  
  346.  
  347. void
  348. do_modified()
  349. {
  350.     register ED *ep = Ep;
  351.  
  352.     if (av[1][0]) {
  353.     switch(av[1][1] & 0x1F) {
  354.     case 'n' & 0x1F:
  355.         ep->Modified = 1;
  356.         break;
  357.     case 'f' & 0x1F:
  358.         ep->Modified = 0;
  359.         break;
  360.     case 'o' & 0x1F:
  361.         ep->Modified = ep->Modified ? 0 : 1;
  362.         break;
  363.     }
  364.     }
  365. }
  366.  
  367.  
  368. void
  369. do_unjustify()
  370. {
  371.     short i, j, waswhite = FALSE;
  372.     ubyte c;
  373.  
  374.  
  375.     for (i = 0; Current[i] == ' '; i++);
  376.     for (j = i; Current[i]; i++) {
  377.     c = Current[j] = Current[i];
  378.     if (c != ' ' || !waswhite)
  379.         j++;
  380.     waswhite = (c == ' ');
  381.  
  382.     }
  383.     Current[j] = 0;
  384.  
  385.     if (i != j) {
  386.     text_sync();
  387.     text_redisplaycurrline();
  388.     }
  389. }
  390.  
  391.  
  392. void
  393. do_justify()
  394. {
  395.     ED *ep = Ep;
  396.     short firstnb, lastnb, i, n, fill, c, sp;
  397.     short changed = FALSE;
  398.  
  399.  
  400.     switch(av[1][0]) {
  401.     case 'c':
  402.     break;
  403.     case 'f':
  404.     firstnb = firstns(Current);
  405.     lastnb = lastns(Current);
  406.     if (firstnb < lastnb && ep->Margin < 255) {
  407.         n = 0;
  408.         i = firstnb;
  409.         while (i <= lastnb) {
  410.         while ((c = Current[i]) && c != ' ')
  411.             i++;
  412.         if (i <= lastnb) {
  413.             n++;
  414.             while (Current[i] == ' ')
  415.             i++;
  416.         }
  417.         }
  418.         fill = ep->Margin - lastnb - 1;
  419.         i = firstnb;
  420.         Current[lastnb + 1] = 0;
  421.         if (n > 0 && fill > 0)
  422.         changed = TRUE;
  423.         while (n > 0 && fill > 0 && Current[i]) {
  424.         while ((c = Current[i]) && c != ' ')
  425.             i++;
  426.         sp = fill / n;
  427.         movmem(&Current[i], &Current[i + sp], strlen(&Current[i]) + 1);
  428.         memset(&Current[i], ' ', sp);
  429.         while (Current[i] == ' ')
  430.             i++;
  431.         fill -= sp;
  432.         n--;
  433.         }
  434.     }
  435.     break;
  436.     default:
  437.     break;
  438.     }
  439.  
  440.     if (changed) {
  441.     text_sync();
  442.     text_redisplaycurrline();
  443.     }
  444. }
  445.  
  446.  
  447. void
  448. do_title()
  449. {
  450.     static ubyte wtitle[256];
  451.  
  452.     strncpy(wtitle, av[1], 255);
  453.     wtitle[255] = 0;
  454.     title(wtitle);
  455. }
  456.  
  457.  
  458.